package cn.changgou.controller;

import cn.changgou.goods.pojo.Brand;
import cn.changgou.service.BrandService;
import com.github.pagehelper.PageInfo;
import cn.changgou.entity.Result;
import cn.changgou.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/brand")
@CrossOrigin
/*
* 跨域：A域名访问B域名的数据（域名或者请求端口或者协议不一致就是跨域了）
* */
public class BrandController {

    @Autowired
    private BrandService brandService;

    /*根据分类ID查询品牌集合-分类ID*/
    @RequestMapping(value = "/category/{id}", method = RequestMethod.GET)
    public Result<List<Brand>> findBrandByCategory(@PathVariable("id") Integer id){
        List<Brand> brands = brandService.findByCategory(id);
        return new Result<List<Brand>>(true, StatusCode.OK, "查询成功！", brands);
    }

    /*分页+条件查询实现*/
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.POST)
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page")Integer page,
                                            @PathVariable(value = "size")Integer size,
                                            @RequestBody Brand brand){
        PageInfo<Brand> pageInfo = brandService.findPage(brand, page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页+条件查询成功！", pageInfo);
    }

    /*分页查询实现*/
    @RequestMapping(value = "/search/{page}/{size}", method = RequestMethod.GET)
    public Result<PageInfo<Brand>> findPage(@PathVariable(value = "page")Integer page,
                                    @PathVariable(value = "size")Integer size){
        PageInfo<Brand> pageInfo = brandService.findPage(page, size);
        return new Result<PageInfo<Brand>>(true, StatusCode.OK, "分页查询成功！", pageInfo);
    }

    /*根据品牌信息多条件搜索*/
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    public Result<List<Brand>> findList(@RequestBody Brand brand){
        List<Brand> brands = brandService.findList(brand);
        return new Result<List<Brand>>(true, StatusCode.OK, "条件搜索品牌成功！", brands);
    }

    /*根据ID删除品牌*/
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable("id")Integer id){
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除品牌成功！");
    }

    /*根据ID修改品牌数据*/
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public Result update(@PathVariable("id") Integer id, @RequestBody Brand brand){
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改品牌成功！");
    }

    /*增加品牌*/
    @RequestMapping(method = RequestMethod.POST)
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "新增品牌成功！");
    }

    /*根据主键ID查询*/
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Result<Brand> findById(@PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK, "根据ID查询Brand成功！", brand);
    }

    /*查询所有品牌*/
    @GetMapping
    public Result<List<Brand>> findAll(){
        //查询所有品牌
        List<Brand> brands = brandService.findAll();
        //响应结果封装
        return new Result<List<Brand>>(true, StatusCode.OK, "查询品牌集合成功！", brands);
    }

}
